package com.sg.service.biz.pbff.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.req.BffQueryTaskCategoriesListBorderReqDto;
import com.sg.dto.biz.pbff.res.BffQueryTaskCategoriesListBorderRespDto;
import com.sg.dto.integration.framework.QueryTaskCategoriesListComReqDto;
import com.sg.dto.integration.framework.QueryTaskCategoriesListComRespDto;
import com.sg.service.biz.pbff.OrgDictionaryService;
import com.wicket.okrframework.integration.OrgDictionaryService;
import com.wicket.okrpbff.common.dto.TreeNode;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class OrgDictionaryServiceImpl implements OrgDictionaryService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.framework.OrgDictionaryService fwBaseOrgDictionaryClient;

    /**
     * B1查询任务分类列表(边界)[10248]
     * gen by moon at 10/3/2024, 2:26:38 PM
     */
    @Trace(operationName = "B1查询任务分类列表(边界)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryTaskCategoriesListBorderRespDto queryTaskCategoriesListBorder(BffQueryTaskCategoriesListBorderReqDto reqDto) {


        QueryTaskCategoriesListComRespDto queryTaskCategoriesListComRespDto_1 = null;
//步骤0: D1查询任务分类列表(公共) - queryTaskCategoriesListCom
        QueryTaskCategoriesListComRespDto queryTaskCategoriesListComRespDto = null;
        QueryTaskCategoriesListComReqDto queryTaskCategoriesListComReqDto = new QueryTaskCategoriesListComReqDto();
        queryTaskCategoriesListComReqDto.setIsSystemData("FALSE");//sourceId:2012911_1
        queryTaskCategoriesListComReqDto.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:2012915_1
        queryTaskCategoriesListComReqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:2012916_1
        if (reqDto != null) {
            queryTaskCategoriesListComReqDto.setParentTypeDictionaryCode(reqDto.getParentTypeDictionaryCode());//SimpleFieldAssign//sourceId:2012923_1
            queryTaskCategoriesListComReqDto.setTypeDictionaryName(reqDto.getTypeDictionaryName());//SimpleFieldAssign//sourceId:2012909_1
            queryTaskCategoriesListComReqDto.setThemeCode(reqDto.getThemeCode());//SimpleFieldAssign//sourceId:2012912_1
        }

        /*D1查询任务分类列表(公共)[10030]   */
        Assert.isNull(queryTaskCategoriesListComReqDto.getThemeCode(), "B1查询任务分类列表(边界)-D1查询任务分类列表(公共)-主题标识不能为空", false);
        Assert.isNull(queryTaskCategoriesListComReqDto.getIsSystemData(), "B1查询任务分类列表(边界)-D1查询任务分类列表(公共)-是否系统数据不能为空", false);
        Assert.isNull(queryTaskCategoriesListComReqDto.getSpaceId(), "B1查询任务分类列表(边界)-D1查询任务分类列表(公共)-创建于空间ID不能为空", false);
        queryTaskCategoriesListComRespDto = fwBaseOrgDictionaryClient.queryTaskCategoriesListCom(queryTaskCategoriesListComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryTaskCategoriesListComRespDto_1 = queryTaskCategoriesListComRespDto;

        BffQueryTaskCategoriesListBorderRespDto retData = new BffQueryTaskCategoriesListBorderRespDto();
        if (queryTaskCategoriesListComRespDto_1 != null) {
            retData.setTaskCategoriesList(queryTaskCategoriesListComRespDto_1.getTaskCategoriesList().stream().map(item -> BeanUtil.toBean(item, BffTaskCategoriesDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:2012922_1
        }


        List<Object> allNodes = new ArrayList<>();
        allNodes.addAll(retData.getTaskCategoriesList());
        List<TreeNode> nodeList = CommonFunctionHelper.buildTree(allNodes);
        retData.setTreeNode(nodeList);
        retData.setTaskCategoriesList(null);

        return retData;
    }
    //
}
